import emoji_List from './emojiList';
import online_Emoji from './onlineEmoji';

export function richText(msg) {
	let content = msg.content;
	if (msg.type == 'img') {
		content = '[图片]';
	} else if (msg.type == 'voice') {
		content = '[语音]';
	}
	return content;
}

//替换表情符号为图片
export function replaceEmoji(content) {
	let replacedStr = content.replace(/\[([^(\]|\[)]*)\]/g, (item, index) => {
		for (let i = 0; i < emoji_List.length; i++) {
			let row = emoji_List[i];
			for (let j = 0; j < row.length; j++) {
				let EM = row[j];
				if (EM.alt == item) {
					//在线表情路径，图文混排必须使用网络路径，请上传一份表情到你的服务器后再替换此路径
					//比如你上传服务器后，你的100.gif路径为https://www.xxx.com/emoji/100.gif 则替换onlinePath填写为https://www.xxx.com/emoji/
					let onlinePath = 'https://s2.ax1x.com/2019/04/12/';
					let imgstr = '<img src="' + onlinePath + online_Emoji[EM.url] + '">';
					return imgstr;
				}
			}
		}
	});
	return '<div style="display: flex;align-items: center;word-wrap:break-word;">' + replacedStr + '</div>';
}
//处理图片尺寸，如果不处理宽高，新进入页面加载图片时候会闪
export function setPicSize(content) {
	// 让图片最长边等于设置的最大长度，短边等比例缩小，图片控件真实改变，区别于aspectFit方式。
	let maxW = uni.upx2px(350); //350是定义消息图片最大宽度
	let maxH = uni.upx2px(350); //350是定义消息图片最大高度
	if (content.w > maxW || content.h > maxH) {
		let scale = content.w / content.h;
		content.w = scale > 1 ? maxW : maxH * scale;
		content.h = scale > 1 ? maxW / scale : maxH;
	}
	return content;
}
